<?php
class User_model extends CI_Model {

	public $fields = array('user_id', 'user_mid', 'password', 'first_name', 'last_name', 'email', 'user_type', 'created_date');
	const table = 'user';
	
    function __construct()
    {
        parent::__construct();
    }
	
	public function add($params)
	{
		// init data
		$data = $this->__initData($this->fields, $params);
		
		if(empty($data)) return false;
		
		$rs = $this->db->insert(self::table, $data);
		
		return $rs;
	}
	
	public function delete($id)
	{
		if($id < 0) return false;
		
		$rs = $this->db->delete(self::table, array(self::table.'_id' => $id));
		
		return $rs;
	}
	
	public function update($params, $id)
	{
		$data = $this->__initData($this->fields, $params);
		
		if($id <= 0 || empty($data)) return false;
		
		//$this->db->where(self::table.'_id', $id);
		$rs = $this->db->update(self::table, $data, array(self::table.'_id', $id));
		
		return $rs;
	}
	
	public function selectId($id) 
	{
		if($id <= 0) return false;
		
		$row = array();
		
		$query = $this->db->get_where(self::table, array('user_id' => $id), 1, 0);
		
		if($query->num_rows() > 0) {
			$row = $query->row();
		}
		
		return $row;
	}
	
	public function selectList($where = array(), $order = array(), $limit = 0, $offset = 0)
	{
		$data = array();
		if(!empty($where)) {
			$this->db->where($where);
		}
		// count item
		$data['total'] = $this->db->count_all_results(self::table);
		
		
		if(!empty($where)) {
			$this->db->where($where);
		}
		
		// order by
		if(!empty($order)) {
			$this->db->order_by($order);
		}
		
		// limit
		if($limit > 0) {
			$this->db->limit($limit, $offset);
		}
		
		$data['data'] = $this->db->get(self::table)->result();
		
		return $data;
	}
}
?>